package com.xy.baselibrary.base

import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.SPUtils
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.disposables.Disposable
import java.lang.Exception
import java.lang.ref.Reference

/**
 * @ClassName BaseModel
 * @Author xy
 * @Date 2021-8-24 0024 21:39
 * @Description  F 代表缓存的数据、T
 */

abstract class MvvmBaseModel<T>: MvvmNetworkObserver<T> {
    lateinit var compositeDisposable: CompositeDisposable
    lateinit var mData: BaseCacheData<T>
    private lateinit var mCachedPreferenceKey: String  //缓存的key
    private lateinit var mApkPredefinedData: String     //缓存的数据
    private  var mIsPaging: Boolean = false //是否分页
    private lateinit var clazz: Class<T>   //将缓存的数据
    var pageNumber = 1

    constructor()
    constructor(
        mCachedPreferenceKey: String,
        mApkPredefinedData: String,
        mIsPaging: Boolean,
        clazz: Class<T>
    ):this() {
        this.mIsPaging = mIsPaging
        this.clazz = clazz
        this.mCachedPreferenceKey = mCachedPreferenceKey
        this.mApkPredefinedData = mApkPredefinedData
        mData = BaseCacheData()
    }



    //刷新
     fun refresh(){
        if (mIsPaging){
            pageNumber = 1
        }
        load()
    }

    //加载数据
     abstract fun load()

    private fun isNeedToUpdate(): Boolean {
        return true
    }

    fun cancel() {
        if (compositeDisposable != null && !compositeDisposable.isDisposed) {
            compositeDisposable.dispose()
        }
    }

    fun addDisposable(d: Disposable) {
        if (d == null) {
            return
        }
        if (compositeDisposable == null) {
            compositeDisposable = CompositeDisposable()
        }
        compositeDisposable.add(d)
    }



    //加载缓存数据
    fun getCacheDataAndLoad() {
        if (mCachedPreferenceKey != null) {
            var saveDataString = SPUtils.getInstance().getString(mCachedPreferenceKey)
            if (saveDataString.isNotEmpty()) {
                try {
                    val saveData = GsonUtils.fromJson<T>(saveDataString, clazz)
                    if (saveData != null) {
                        onSuccess(saveData, true)
                        if (isNeedToUpdate()) {
                            load()
                        }
                        return
                    }
                } catch (e: Exception) {
                    e.printStackTrace()
                }
            }
        }

        if (mApkPredefinedData != null) {
            val saveData = GsonUtils.fromJson<T>(mApkPredefinedData, clazz)
            try {
                if (saveData != null) {
                    onSuccess(saveData, true)
                }
            } catch (e: Exception) {
                e.printStackTrace()
            }

        }
        load()
    }

    //注册监听
    fun register(){

    }

    //是否分页
    abstract fun isPaging():Boolean

    //缓存预制数据  网络请求后会进行缓存 ，防止没有网络后出现空白的界面
    fun saveDataToPreference(data: T) {
        if (data != null) {
            mData.data = data;
            mData.cacheTime = System.currentTimeMillis()
            SPUtils.getInstance().put(mCachedPreferenceKey,GsonUtils.toJson(mData))
        }
    }


    //通知UI线程
    fun notifyResultToListener(networkRespondBean:T,data: T,isFromCache:Boolean){

        if (isPaging()){
            if (mCachedPreferenceKey!=null&&pageNumber==1&&!isFromCache){
                saveDataToPreference(networkRespondBean) //缓存第一页数据
            }

            if (!isFromCache){ //不是来自于缓存
                if (data!==null&&data is MutableList<*>){
                    pageNumber++
                }
            }
        }else{
            if (mCachedPreferenceKey!=null&&!isFromCache){
                saveDataToPreference(networkRespondBean)
            }
        }


    }

}